<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
		<meta name="format-detection" content="telephone=no" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-touch-fullscreen" content="yes"> 
		<meta content="telephone=no,email=no" name="format-detection" />
        <meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
		<title>羊仓健康生活家</title>
		<link rel="stylesheet" href="../css/tijiaoxinxi.css" />
		<link rel="stylesheet" href="../css/LArea.css" />
		<script type="text/javascript" src="../js/flexible.js" ></script>
		<script type="text/javascript" src="../js/jquery.min.js" ></script>
		<script type="text/javascript" src="../js/addressDetails.js" ></script>
		<script type="text/javascript" src="../js/LAreaData1.js" ></script>
		<script type="text/javascript" src="../js/LAreaData2.js" ></script>
		<script type="text/javascript" src="../js/LArea.js" ></script>
		<script type="text/javascript" src="../js/jquery.form.js" ></script>
		<link rel="stylesheet" href="../js/ui-dialog.css" />
		<script type="text/javascript" src="../js/dialog-min.js" ></script>
	</head>
		<body>
		<div class="topTitle">
    <a class="topReturn" onclick="history.go(-1)">
        <img src="../img/baxk.png" alt=""></a>
    <h5 class="title">羊仓会员专属</h5>
</div>
<!-- 表单 -->
<div class="From">
    <form id="form1" method="post" action="">
        <div class="cler">
            <div class="iNcard">姓名</div>
            <input type="text" id="receiptName" class="cardNnumber" name="receiptName" placeholder="" value=""  onkeyup="statname()">

            <div style="clear:both"></div>
        </div>

        <div class="cler">
            <div class="iNcard">手机号码</div>
            <input type="text" id="mobile" class="cardNnumber" name="mobile"  placeholder="" value="" onfocusout="isnum()">
            <div style="clear:both"></div>
        </div>

        <div class="cler">
            <div class="iNcard">请选择省市区</div>
            <input type="text" class="cardNnumber" placeholder="" name="city" value="" id="details" readonly="" >
            <input type="hidden" id="city"    value="">
            <div style="clear:both"></div>
        </div>
        <div class="cler" style="padding-bottom:20px;height:auto;overflow:auto;">
            <div class="iNcard">详细地址</div>
            <input type="hidden" id="addressId"   name="addressId" value="">
            <textarea id="street" name="street" class="carddetails" placeholder="" onkeyup="words_deal()" ></textarea>
            <div style="clear:both"></div>
        </div>

       
        <input type="hidden" name="yieldId" id="yieldId" value="${(fn:length(weekList))>0?weekList[0].yieldId:""} ">
        
        <div class="clers" style="width:100%; height:1.375rem; position: fixed; bottom: 0; left: 0; background-color: #007234;">
            <input type="button" id="submitBt" name="" class="clersubmit" value="确定" onclick="checkForm() " style="background: #007234;">
        </div>

       
       
    </form>
</div>
</body>
<script type="text/javascript">

    /**
     *检查首配周是否有产量
     */
    function checkWeek(){
        var yieldId = $("#choices1").find("option:selected").val();
        $.ajax({
            type:"get",
            url:basePath+"/eggcard/v1/card/checkWeek?yieldId="+yieldId,
            dataType : 'json',
            success:function(data){
                if(data.code==1000) {
                    var flag = data.flag;
                    if(flag){
                        //4、提交到后台
                        addUserAddress();
                    }else{
                        //  产量不够，提示是否接受系统分配  Y: activeCard N :返回重新选择
                        var yyyyMMw = data.yyyyMMw;
//                                getTip(yyyyMMw);
                    }
                }else{
                    malert(data.msg);
                }
            },
            error:function(data) {
            }
        });
    }

    /**
     *激活卡片
     */
    function activeCard(userId , addressId){
//            var yieldId = $("#choices1").find("option:selected").val();
        var yieldId = $("#yieldId").val();
        var isWorkday = $("#choices2").find("option:selected").val();
        $.ajax({
            type:"post",
            url:basePath+"/eggcard/v1/card/activeCard",
            data:{
                "userId":userId,
                "cardNum":cardNum,
                "name":"",
                "mobile":mobile,
                "openId":openId,
                "addressId":addressId,
                "isWorkday":isWorkday,
                "yieldId":yieldId,
                "activeType":0,
                "remark":""
            },
            dataType : 'json',
            success:function(data){
                if(data.code==1000) {
                    var cardPassword = data.data.cardPassword;
                    var orderId = data.data.orderId;
                    var receiptName = data.data.receiptName;
                    var activeMobile = data.data.activeMobile;
                    var receiptPhone = data.data.receiptPhone;
                    var receivingTime = data.data.receivingTime;
                    var receivingAddress = data.data.receivingAddress;
                    var resMsg = data.data.resMsg;
                    toActiveCardOk(cardNum,cardPassword,orderId,activeMobile,receiptName,receiptPhone,receivingTime,receivingAddress,resMsg);
                }else{
                    malert(data.msg);
                }
            },
            error:function(data) {
            }
        });
    }

    /**
     *添加收货地址
     */
    function addUserAddress(){
        var receiptName =$("#receiptName").val();
        var receiptPhone =$("#mobile").val();// 联系方式
        var street =$("#street").val();
        var cityIds =$("#city").val();
        var arr = cityIds.split(',');
        var province= arr[0];
        var city= arr[1];
        var county = arr[2];
        $.ajax({
            type:"post",
            url:basePath+"/eggcard/v1/user/addUserAddress",
            data:{
                "userId":userId,
                "receiptName":receiptName,
                "receiptPhone":receiptPhone,
                "province":province,
                "city":city,
                "county":county,
                "street":street
            },
            dataType : 'json',
            success:function(data){
                if(data.code==1000) {
                    var addressId = data.data.addressId;
                    $("#addressId").val(addressId);
                    // 激活开卡
                    activeCard(userId,addressId);
                }else{
                    malert(data.msg);
                }
            },
            error:function(data) {
            }
        });
    }

    /**
     * 检查页面表单数据
     * @returns {boolean}
     */
    function checkForm(){
        if($("#receiptName").val()==""){
            malert("请输入收货人真实姓名");
            return false;
        }
        if($("#mobile").val()==""){
            malert("请输入收货手机号码或固定电话");
            return false;
        }
        if($("#details").val()==""){
            malert("请选择收货省市区");
            return false;
        }
        if($("#street").val()==""){
            malert("请填写收货详细地址");
            return false;
        }

//          if($("#choices1").val()==0){
//              malert("请选择收货时间");
//              return false;
//          }

        if($("#choices2").val()==0){
            malert("请选择收货时间");
            return false;
        }
        if(!checkAddr()){
            malert("请选择收货地址");
            return false;
        }
//       todo     checkWeek();
        addUserAddress();
    }

    /**
     * 跳转到成功页面
     * @param userId
     * @param cardNum
     * @param mobile
     */
    function toActiveCardOk(cardNum,cardPassword,orderId,activeMobile,receiptName,receiptPhone,receivingTime,receivingAddress,resMsg){
        window.location.href= basePath+"/eggcard/h5/toActiveCardOk?userId="+userId+"&openId="+openId+"&cardNum="+cardNum+"&cardPassword="+cardPassword+"&orderId="+orderId+"&activeMobile="+activeMobile+"&receiptName="+receiptName+"&receiptPhone="+receiptPhone+"&receivingTime="+receivingTime+"&receivingAddress="+receivingAddress+"&resMsg="+resMsg;
    }

    function malert(msg){
        var d=dialog({title:'提示',content:msg});
        d.showModal();
    }

</script>
<script type="text/javascript">
    var area = new LArea();
    area.init({
        'trigger': '#details',
        'valueTo': '#city',
        'keys': {
            id: 'value',
            name: 'text'
        },
        'type': 2,
        'data': [provs_data, citys_data, dists_data]
    });

    function checkAddr(){
        content=$("#details").val();
        if(content.indexOf("请选择")>=0){
            $("#details").val("请选择收货省市区");
            return false;
        }
        return true;
    }
</script>
<!--手机固话验证-->
<script type="text/javascript">
     function isnum(){
         var isPhone = /^((0\d{2,3}))(\d{7,8})$/;

        var isMob = /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/;
        var value=document.getElementById("mobile").value.trim();
        if(isPhone.test(value) || isMob.test(value) ){
            return true;
        }else{
            malert("请输入正确的手机号码或固定电话（示例01085458545）");
            return false;
           
        }
    }
</script>

<!--收货人-->
<script>
    function statname(){
        var curLength=$("#receiptName").val().length;
        if(curLength>10){
//          var num=$("#receiptName").val().substr(0,10);
//          $("#receiptName").val(num);
            malert("收货人姓名不超过10个字，请重新输入！" );
            return false;
        }
    }
</script>
<!--详情地址验证-->
<script>
    function words_deal(){
        var curLength=$("#street").val().length;
        if(curLength>50){
            var num=$("#street").val().substr(0,50);
            $("#street").val(num);
            malert("详细地址不超过50个字，请重新输入！" );
            return false;

        }
    }
</script>
<!--提示信息-->
<script>
    function getTip(yyyyMMw){
        $(".bot-txt").show();
        $("#tipYyyyMMw").html(yyyyMMw);
    }

    $('.no').click(function(){
        $(".bot-txt").hide();
    });

    $('.yes').click(function(){
        $(".bot-txt").hide();
        addUserAddress();

    });
</script>

	</body>
</html>
